package indigo.captcha.image;

import java.awt.image.BufferedImage;
import java.security.SecureRandom;
import java.util.Random;

import com.jhlabs.image.InvertFilter;

public class RandomInvertDeformer extends ThreadLocalFilterDeformer<InvertFilter> {
	@Override
	protected InvertFilter createFilter() {
		return new RandomInvertFilter();
	}

	@Override
	protected void prepareFilter(InvertFilter filter) {
		// noop
	}

	public static class RandomInvertFilter extends InvertFilter {
		private final Random random = new SecureRandom();
		private boolean invert;

		@Override
		public BufferedImage filter(BufferedImage src, BufferedImage dst) {
			invert = random.nextBoolean();
			return super.filter(src, dst);
		}

		@Override
		public int filterRGB(int x, int y, int rgb) {
			return invert ? super.filterRGB(x, y, rgb) : rgb;
		}
	}
}
